package com.dy.lock.screen.parse;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Parser {
	private static Map<String, BeanVO> parserMap = new HashMap<String, BeanVO>();

	public Map<String, BeanVO> getParserMap() throws Exception {
		if (parserMap.isEmpty())
			this.parser();
		return parserMap;
	}

	@SuppressWarnings("unchecked")
	private Map<String, BeanVO> parser() throws Exception {
		SAXReader reader = new SAXReader();
		Document document = reader.read(Parser.class.getClassLoader()
				.getResourceAsStream("bean.xml"));
		Document doc = document.getDocument();
		Element root = doc.getRootElement();
		Map<String, BeanVO> map = new HashMap<String, BeanVO>();
		for (Iterator<Element> i = root.elementIterator(); i.hasNext();) {
			Element element = i.next();
			BeanVO bean = new BeanVO();
			List<Attribute> list = element.attributes();
			for (Attribute att : list) {
				if ("code".equals(att.getName()))
					bean.setCode(att.getValue());
				else if ("id".equals(att.getName()))
					bean.setId(att.getValue());
				else if ("method".equals(att.getName()))
					bean.setMethod(att.getValue());
			}
			map.put(bean.getCode(), bean);
		}
		parserMap = map;
		return map;
	}
}
